package class08_greaterHeap;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;

public class Test {
    public static  boolean isHeap(GreaterHeap heap){
        int size = heap.size();
        Comparator comp = heap.getComp();
        for(int i = 1; i<size; i++){
            int father = (i-1)/2;
            if(comp.compare(heap.getElemByIndex(father),heap.getElemByIndex(i))<0)
                return false;
        }
        return true;
    }

    public static int[] randomArr (int maxLen,int maxValue)
    {
        int len = (int)(maxLen*Math.random());
        int[] arr =new int[len];

        for(int i = 0; i<len; i++)
        {
            arr[i] = (int)(maxValue*Math.random());
        }
        return arr;
    }
    public static int[] randomArrNodup(int maxLen, int maxValue){//没有重复值的随机数组
        int[] arr = randomArr(maxLen,maxValue);
        HashSet<Integer> hs = new HashSet<>();
        for (int i:arr) {
            hs.add(i);
        }

        int[] res = new int[hs.size()];
        int counter = 0;
        for(Integer i : hs){
            res[counter++] = i;
        }

        return res;
    }



    public static void main(String[] args) {
        GreaterHeap<Integer> heap = new GreaterHeap<>(new Comparator<Integer>(){
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });
        int[] arr = randomArrNodup(100,100);
        for (int num:arr) {
            heap.push(num);
        }
        System.out.println(isHeap(heap));
        heap.pop();
        System.out.println(isHeap(heap));

        ArrayList<Integer> temp = (ArrayList<Integer>)heap.getHeap().clone();
        for(Integer elem : temp){
            double p = Math.random();
            if(p>0.7){
                heap.removeByValue(elem);
            }
        }
        System.out.println(isHeap(heap));

        for(Integer elem: heap.getHeap()){
            System.out.print(elem+" ");
        }
    }
}
